import 'thinkjs3-ts';
import path from 'path';
import nunjucks from 'think-view-nunjucks';
import fileSession from 'think-session-file';
import fileCache from 'think-cache-file';
import mysql from 'think-model-mysql';
import { Console, DateFile } from 'think-logger3';
import socketio from 'think-websocket-socket.io';
import moment from 'moment'

const Env = think.env === "development";

export const cache = {
      type: 'file',
      common: {
            timeout: 24 * 60 * 60 * 1000 // millisecond
      },
      file: {
            handle: fileCache,
            cachePath: path.join(think.ROOT_PATH, 'runtime/cache'), // absoulte path is necessarily required
            pathDepth: 1,
            gcInterval: 24 * 60 * 60 * 1000 // gc interval
      }
};


export const session = {
      type: 'file',
      common: {
            cookie: {
                  name: 'thinkjs'
                  // keys: ['werwer', 'werwer'],
                  // signed: true
            }
      },
      file: {
            handle: fileSession,
            sessionPath: path.join(think.ROOT_PATH, 'runtime/session')
      }
};

export const view = {
      type: 'nunjucks',
      common: {
            viewPath: path.join(think.ROOT_PATH, 'view'),
            sep: '_',
            extname: '.html'
      },
      nunjucks: {
            handle: nunjucks
      }
};


export const model = {
      type: 'mysql',
      common: {
            logConnect: true,
            logSql: true,
            logger: (msg: string) => think.logger.info(msg)
      },
      mysql: {
            handle: mysql,
            user: 'liyh', // 用户名
            password: 'D2adYlcixbJafU8T', // 密码
            database: 'liyh', // 数据库
            host: 'rm-bp11q43weusqf58w57o.mysql.rds.aliyuncs.com', // host
            port: 3306, // 端口
            connectionLimit: 1, // 连接池的连接个数，默认为 1
            prefix: 'm_', // 数据表前缀，如果一个数据库里有多个项目，那项目之间的数据表可以通过前缀来区分
            acquireWaitTimeout: 10000,
            charset: 'utf8mb4',
      },
}


// export const logger = {
//       type: Env ? 'sole' : 'dateFile',
//       sole: {
//             handle: Console,
//       },
//       dateFile: {
//             handle: DateFile,
//             level: 'ALL',
//             absolute: true,
//             pattern: '-yyyy-MM-dd',
//             alwaysIncludePattern: false,
//             filename: path.join(think.ROOT_PATH, `logs/${moment().format('Y/MM/DD')}.log`),
//       }
// };

export const websocket = {
      type: 'socketio',
      common: {
      },
      socketio: {
            handle: socketio,
            // allowOrigin: 'http://127.0.0.1:5500/',  // 默认所有的域名都允许访问
            path: '/socket.io',             // 默认 '/socket.io'
            messages: {
                  open: '/websocket/open', //建立连接时处理对应到 websocket Controller 下的 open Action
                  close: '/websocket/close', // 关闭连接时处理的 Action
                  join: '/websocket/join',
                  room: '/websocket/room',
                  addUser: '/websocket/addUser',
            }
      }
}